package com.qhs.sys.service.impl;

import com.qhs.sys.constant.TreeView;
import com.qhs.sys.mapper.SysRoleInfoMapper;
import com.qhs.sys.entity.SysRoleInfo;
import com.qhs.sys.service.RoleService;
import com.qhs.framework.utils.DateUtil;
import com.qhs.framework.utils.IDWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by xinchao on 2016/11/30 0030.
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private SysRoleInfoMapper sysRoleInfoMapper;
    @Autowired
    private IDWorker idWorker;


    @Override
    public List<SysRoleInfo> findRoleListByUserName(String username) {

        return sysRoleInfoMapper.findRolesByUser(username);
    }

    @Override
    public List<SysRoleInfo> findRoleByWhere(SysRoleInfo record) {
        return sysRoleInfoMapper.findRoleByWhere(record);
    }

    @Override
    public int authRole(Long[] ids, Long userId) {
        List list = new ArrayList();
        for(Long id:ids){
            HashMap map = new HashMap();
            map.put("id",idWorker.generateId());
            map.put("userId",userId);
            map.put("roleId",id);
            list.add(map);
        }
        sysRoleInfoMapper.deletePermission(userId);
        return sysRoleInfoMapper.authRole(list);
    }

    @Override
    public int deleteByPrimaryKey(Long roleId) {
        return sysRoleInfoMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public int insert(SysRoleInfo record) {
        record.setCreateTime(DateUtil.getDefaultDate());
        record.setRoleId(idWorker.generateId());
        return sysRoleInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(SysRoleInfo record) {
        return sysRoleInfoMapper.insertSelective(record);
    }

    @Override
    public SysRoleInfo selectByPrimaryKey(Long roleId) {
        return sysRoleInfoMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public int updateByPrimaryKeySelective(SysRoleInfo record) {
        return sysRoleInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SysRoleInfo record) {
        return sysRoleInfoMapper.updateByPrimaryKey(record);
    }
    @Override
    public  List<TreeView> getTreeView(String userName){
        List<TreeView> treeViews = new ArrayList<>();
        List<SysRoleInfo> allRole = sysRoleInfoMapper.findRoleByWhere(new SysRoleInfo());
        List<SysRoleInfo> roleList = sysRoleInfoMapper.findRolesByUser(userName);
        for (SysRoleInfo role: allRole ) {
            TreeView treeView = new TreeView();
            treeView.setText(role.getRoleName());
            treeView.setTags(String.valueOf(role.getRoleId()));
            HashMap<String,Object> obj = new HashMap();
            obj.put("checked",isCheck(role.getRoleId(),roleList));
            treeView.setState(obj);
            treeViews.add(treeView);
        }
        return treeViews;
    }
    public boolean isCheck(Long id,List<SysRoleInfo> list){
        for(SysRoleInfo sysRoleInfo:list){
            if(id.longValue()==sysRoleInfo.getRoleId().longValue()){
                return true;
            }
        }
        return false;
    }
    @Override
    public int authMenu(Long[] ids,Long roleId){
        List list = new ArrayList();
        for(Long id:ids){
            HashMap map = new HashMap();
            map.put("id",idWorker.generateId());
            map.put("roleId",roleId);
            map.put("menuId",id);
            list.add(map);
        }
        sysRoleInfoMapper.deleteMenu(roleId);
        return sysRoleInfoMapper.authMenu(list);
    }

    @Override
    public long getRoleId(String code) {
        return sysRoleInfoMapper.getRoleId(code);
    }

    @Override
    public List<String> findRoleListByUserId(Long userId) {
        return sysRoleInfoMapper.findRoleListByUserId(userId);
    }
}
